<?php
namespace App\Repositories;

use App\Services\Tools\CommonService;


class IndexRepository
{
    /**
     * 查询后台首页数据
     * @return array
     */
    public function getStatisticsData()
    {
        $time = CommonService::getTodayTime();
        // 今日提现统计
        $userWithdrawData = [];
        $userWithdrawList = app('model_user_withdraw')->whereBetween('add_time',[$time['start_at'],$time['end_at']])
            ->select('money','status')->get();
        if (!empty($userWithdrawList)) {
            foreach ($userWithdrawList as &$value) {
                if (!isset($userWithdrawData[$value->status])) {
                    $userWithdrawData[$value->status] = [
                        'status' => $value->status,
                        'money' => $value->money,
                    ];
                } else {
                    $userWithdrawData[$value->status]['money'] += $value->money;
                }
            }
            ksort($userWithdrawData);
            $userWithdrawData = array_merge($userWithdrawData);
        }

        // 今日公开房间数
        $userRoomTotal = app('model_user_room')->whereBetween('add_time',[$time['start_at'],$time['end_at']])->count();

        // 当前在线人数
        $onlineUserTotal = app('repo_user')->getOnlineUserCount();

        // 今日注册用户
        $registerUserTotal = app('model_user')->whereBetween('register_time',[$time['start_at'],$time['end_at']])->count();

        // 今日充值统计
        $recharge_list = app('model_user_recharge')->whereBetween('add_time',[$time['start_at'],$time['end_at']])
            ->select('money','type','status')->get();
        $rechargeChannelData = [];
        $paid = $unpaid = 0;
        if (!empty($recharge_list)) {
            foreach ($recharge_list as &$value) {
                if (!isset($rechargeChannelData[$value->type.$value->status])) {
                    $rechargeChannelData[$value->type.$value->status] = [
                        'type' => $value->type,
                        'status' => $value->status,
                        'count' => 1,
                        'money' => $value->money,
                    ];
                } else {
                    $rechargeChannelData[$value->type.$value->status]['count'] += 1;
                    $rechargeChannelData[$value->type.$value->status]['money'] += $value->money;
                }
                if ($value->status == 2) {
                    $paid += $value->money;
                } else {
                    $unpaid += $value->money;
                }
            }
            ksort($rechargeChannelData);
            $rechargeChannelData = array_values($rechargeChannelData);
        }

        return [
            'paid' => $paid,
            'unpaid' => $unpaid,
            'user_withdraw_data' => $userWithdrawData,
            'user_room_total' => $userRoomTotal,
            'register_user_total' => $registerUserTotal,
            'online_user_total' => $onlineUserTotal,
            'user_recharge_data' => $rechargeChannelData,
        ];
    }
}


?>